package com.webmeeting.jsfbeans.chat.wordchat;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.myframework.utils.logutil.WMLogger;
import com.myframework.utils.stringutil.StringUtil;
import com.webmeeting.chat.wordchat.dao.daomanagers.UserManager;
import com.webmeeting.chat.wordchat.dao.entities.User;

@ManagedBean(name = "indexBean_wordchat")
@RequestScoped
public class IndexBean {

	// used to login, which get from the input text
	private String	username;
	// used to login, which get from the input text
	private String	password;
	// used to save the user which retrieved from database successfully
	private User		user;
	// the status of login (failed or successful)
	private boolean	login;
	// header message
	private String	headerMsg;

	public boolean isLogin() {
		return login;
	}

	public void setLogin(boolean login) {
		if (StringUtil.isEmpty(this.username) || StringUtil.isEmpty(this.password)) {
			this.headerMsg = "Username and password can not be empty!";
			this.login = false;
			return;
		}

		User user = UserManager.findUserByName(this.username);
		if (null == user) {
			WMLogger.debug("No user existing in this system with username: " + username);
			this.headerMsg = "No user existing in this system with username: " + username;
			this.login = false;
			return;
		}
		if (!this.password.equals(user.getPassword())) {
			WMLogger.debug("Password is not correct.");
			this.headerMsg = "Password is not correct";
			this.login = false;
			return;
		}

		this.user = user;
		this.headerMsg = "Welcome to chat room, " + username;
		this.login = true;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getHeaderMsg() {
		return headerMsg;
	}

	public void setHeaderMsg(String headerMsg) {
		this.headerMsg = headerMsg;
	}

}
